// fs模块
const fs = require('fs');
const path = require('path');
const sql = require('../mysql/config')
const { output,input } = require('../until/crypto');
module.exports = async (req,res)=>{
    console.log(req.session.user,"----------------uploads------------");
    var { hiddenUsername, nickname, oldPassword, newPassword } = req.body;
    console.log("==============",req.body);
    // 第1种方案

    // var user = await sql(`select nickname,password,avatar from user where username = "${hiddenUsername}"`);
    // var imagesSrc = ''
    // var alert = '';
    // if (req.file) {
    //     var oldPath = req.file.path;
    //     var newPath = req.file.destination + req.file.originalname;
    //     // 上传图片重命名
    //     fs.renameSync(oldPath,newPath); 
    //     // 把图片的服务端的请求路径存储到数据库中
    //     imagesSrc = 'img/'+ req.file.originalname;
    //     alert += "头像更新成功";
    // }

    // // 密码判断
    // if (oldPassword && newPassword) {
    //     var password = await sql(`select password from user where username = "${hiddenUsername}"`); // [{password:13123123}]
    //     if (oldPassword === input(password[0].password)) {
    //         await sql(`update user set password = "${output(newPassword)}" where username = "${hiddenUsername}"`); 
    //         alert += "密码修改成功";           
    //     }else{
    //         alert += "旧密码修改错误"; 
    //     }
    // }
    // imagesSrc = imagesSrc === ''?user[0].avatar:imagesSrc;
    // nickname = nickname === ''?user[0].nickname:nickname;  
    // await sql(`update user set avatar = "${imagesSrc}", nickname = "${nickname}" where username = "${req.body.hiddenUsername}"`); 
    // res.send(`<script>alert("${alert}");location.href="/"</script>`)


    // 第2种方案
    var alert = '';
    // 1.判断是否有图片上传
    if (req.file) {
        var oldPath = req.file.path;
        // 不会去重
        // var newPath = req.file.path + path.extname(req.file.originalname);
        // 会去重，不能上传相同名称的图片
        var newPath = req.file.destination + req.file.originalname;
        // 上传图片重命名
        fs.renameSync(oldPath,newPath);
        // 把图片的服务端的请求路径存储到数据库中
        var imgSrc = 'img/'+ req.file.originalname;
        await sql(`update user set avatar = "${imgSrc}" where username = "${req.body.hiddenUsername}"`);  
        alert +=  "图片修改成功";   
    }

    // 2.判断用户是否修改昵称
    if (nickname) {
        await sql(`update user set nickname = "${nickname}" where username = "${hiddenUsername}"`);
        alert +=  " 昵称修改成功 ";
    }

    // 3.判断用户是否修改密码
    if (oldPassword && newPassword) {
        var password = await sql(`select password from user where username = "${hiddenUsername}"`); // [{password:13123123}]
        if (oldPassword === input(password[0].password)) {
            await sql(`update user set password = "${output(newPassword)}" where username = "${hiddenUsername}"`);            
            alert +=  " 密码修改成功 ";
        }else{
            alert +=  " 旧密码输入错误 ";
        }
    }

    res.send(`<script>alert("${alert}");location.href="/"</script>`);

}